//
//  YSPNewFeatureViewController.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/19.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YSPNewFeatureViewController: UICollectionViewController {
    //设置流水布局
    private let layout = UICollectionViewFlowLayout()
    
    //设置cell的个数
    private let itemCount = 4
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }
    
    private func prepareCollectionView() {
        //注册cell
        //YSPNewFeatureCollectionViewCell.self等价于[YSPNewFeatureCollectionViewCell class]
        collectionView?.registerClass(YSPNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置布局参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YSPNewFeatureCollectionViewCell
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //正在显示的所有cell的index
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        if visibleIndexPath.item == itemCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! YSPNewFeatureCollectionViewCell
            cell.startButtonAnimation()
        }
    }
  

}

//MARK: - 新特性界面的cell
class YSPNewFeatureCollectionViewCell: UICollectionViewCell {
    //MARK: - 属性
    var index = 0 {
        didSet {
            //赋不同的值就显示不同的图片
            bgView.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    func startButtonAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //将scale恢复成默认的1，1
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("完成所有动画")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
        addSubview(bgView)
        addSubview(startButton)
        
        startButton.translatesAutoresizingMaskIntoConstraints = false
        bgView.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        
        //bgView
        //top
        addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //bottom
        addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //leading
        addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //trailing
        addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //startButton
        //centerX = contentView.centerX
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //centerY = contentVIew.centerY + 200
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 200))
        
    }
    
    //MARK: - 懒加载
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "doWhenStartBtnDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    @objc private func doWhenStartBtnDidClicked() {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(true)
    }
}











